package ${tempValues.package_this_file};

import org.apache.dubbo.common.status.Status;
import org.apache.dubbo.common.status.StatusChecker;
import org.apache.dubbo.common.status.support.MemoryStatusChecker;
import org.apache.dubbo.config.spring.status.DataSourceStatusChecker;
import org.apache.dubbo.config.spring.status.SpringStatusChecker;
import org.apache.dubbo.registry.status.RegistryStatusChecker;
import org.apache.dubbo.rpc.protocol.dubbo.status.ServerStatusChecker;
import org.apache.dubbo.rpc.protocol.dubbo.status.ThreadPoolStatusChecker;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DubboStatusChecker implements InitializingBean {
    private StatusChecker memoryStatusChecker;

    private StatusChecker serverStatusChecker;

    private StatusChecker threadPoolStatusChecker;

    private StatusChecker registryStatusChecker;

    private StatusChecker springStatusChecker;

    private StatusChecker dataSourceStatusChecker;

    public Status checkMemory() {
        return memoryStatusChecker.check();
    }

    public Status checkServer() {
        return serverStatusChecker.check();
    }

    public Status checkThreadPool() {
        return threadPoolStatusChecker.check();
    }

    public Status checkRegistry() {
        return registryStatusChecker.check();
    }

    public Status checkSpring() {
        return springStatusChecker.check();
    }

    public Status checkDataSource() {
        return dataSourceStatusChecker.check();
    }


    public Status allCheck() {
        Status status = checkMemory();
        if (!isOk(status)) {
            return status;
        }
        status = checkServer();
        if (!isOk(status)) {
            return status;
        }
        status = checkThreadPool();
        if (!isOk(status)) {
            return status;
        }
        status = checkRegistry();
        if (!isOk(status)) {
            return status;
        }
        status = checkSpring();
        if (!isOk(status)) {
            return status;
        }
//		status = checkDataSource();
//		if (!isOk(status)) {
//			return status;
//		}
        return null;
    }


    public String getAllDetail() {
        List<Status> list = new ArrayList<>();
        list.add(checkMemory());
        list.add(checkServer());
        list.add(checkThreadPool());
        list.add(checkRegistry());
        list.add(checkSpring());
        //list.add(checkDataSource());
        StringBuilder buf = new StringBuilder();
        for (Status st : list) {
            buf.append("status:" + st.getLevel().name());
            buf.append(",");
            buf.append(st.getMessage() != null ? st.getMessage() : "");
            buf.append("<br>\r\n");
        }
        return buf.toString();
    }

    public String healthCheck(String a) {
        StringBuilder buf = new StringBuilder();
        Status status = null;
        if (a.equals("all")) {
            status = allCheck();
        } else if (a.equals("memoryStatusChecker")) {
            status = checkMemory();
        } else if (a.equals("serverStatusChecker")) {
            status = checkServer();
        } else if (a.equals("threadPoolStatusChecker")) {
            status = checkThreadPool();
        } else if (a.equals("registryStatusChecker")) {
            status = checkRegistry();
        } else if (a.equals("springStatusChecker")) {
            status = checkSpring();
        } else if (a.equals("dataSourceStatusChecker")) {
            status = checkDataSource();
        }
        if (status != null) {
            buf.append("status:" + status.getLevel().name());
            buf.append(",");
            buf.append(status.getMessage() != null ? status.getMessage() : "");
        } else {
            buf.append("OK");
        }
        return buf.toString();
    }


    private boolean isOk(Status status) {
        if (!status.getLevel().name().equals("OK")) {
            return false;
        }
        return true;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        memoryStatusChecker = new MemoryStatusChecker();
        serverStatusChecker = new ServerStatusChecker();
        threadPoolStatusChecker = new ThreadPoolStatusChecker();
        registryStatusChecker = new RegistryStatusChecker();
        springStatusChecker = new SpringStatusChecker();
        dataSourceStatusChecker = new DataSourceStatusChecker();
    }
}
